<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_catecontrol extends base {
	function admin_catecontrol(& $get,& $post) 
	{
        $this->base( & $get,& $post);
        $this->load('cate');
    }
    function ondefault($msg='') 
    {
    	//var_dump($this->post);exit();
    	//$job = $this->post['job'];
    	$job = ($this->get[2])?$this->get[2]:$this->post['job'];
    	if($job == 'add')
    	{
    		if(!empty($this->post['catename'])) 
    		{
	            $stype = (int)$this->post['stype'] ;
	            $mid = (int)$this->post['mid'] ;
	            $_ENV['cate']->addsyscate($this->post['catename'],$stype,$mid);
	            //$this->ondefault('添加成功!');
	            $this->update_cache('catesys','sys');
	            msg('添加成功！',"index.php?admin_cate");
	        }
	        else
	        {
	        	msg('系统分类名称不能为空！',"index.php?admin_cate");
	        	//$this->ondefault('系统分类名称不能为空!');
	        }
    	}
    	elseif ($job == 'addcate')
    	{
    		//var_dump($this->post);exit();
    		$cid = $this->post['cid'] ;
            $lines = explode("\n" , $this->post['categorys']);
            $_ENV['cate']->addcid($lines,$cid);
            msg('添加成功！',"index.php?admin_cate/catelist/".$cid);
    	}
    	elseif ($job == 'edit'){
    		$id = (isset($this->get[3]))?$this->get[3]:$this->post['id'];
    		$catedata = $_ENV['cate']->get_syscate_byid($id);
    		$catesys = $_ENV['cate']->list_catesys();
    		include template('catesyslist','admin');
    	}
    	elseif ($job == 'editsave')
    	{
    		$id = $this->post['id'];
    		$name = $this->post['catename'] ;
    		$stype = (int)$this->post['stype'] ;
    		$mid = (int)$this->post['mid'] ;
    		$_ENV['cate']->updatesyscate($name,$stype,$mid,$id);
            msg('编辑成功！',"index.php?admin_cate");
    	}
    	else
    	{
    		$msg && $message=$msg;
    		$catesys = $_ENV['cate']->list_catesys();
        	include template('catesyslist','admin');
    	}
        
    }
    
    function oncatelist()
    {
    	$cid = (isset($this->get[2]))?$this->get[2]:$this->post['id'];
    	$catesys = $_ENV['cate']->list_catesys();
    	$categorylist = $_ENV['cate']->catelist_byid($cid);
    	$msg && $message=$msg;
    	$topname = $_ENV['cate']->get_syscatename($cid);
        include template('catelist','admin');
    }
    function onupcate(){
    	if(isset($this->post['submit'])) 
    	{
    		$cid = $this->post['hiddencid'] ;
            $_ENV['cate']->upcid($this->post['cname']);
            //$this->oncatelists($cid,"编辑成功");
            msg('添加成功！',"index.php?admin_cate/catelist/".$cid);
    	}
    }
    function onreordercate()
    {
    	$orders = explode(",",$this->post['order']);
    	//var_dump($orders);exit();
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $cid) {
            $_ENV['cate']->order_cate(intval($cid),$order);
        }
    }
    function onreordersyscate()
    {
    	$orders = explode(",",$this->post['order']);
    	//var_dump($orders);exit();
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $cid) {
            $_ENV['cate']->order_catesys(intval($cid),$order);
        }
        $this->update_cache('catesys','sys');
    } 
    function ondelcate()
    {
    	$id = (int)$this->get[2];
    	$cid = (int)$this->get[3];
    	$_ENV['cate']->delcid($id);
        msg('删除成功！',"index.php?admin_cate/catelist/".$cid);
    }
    function ondelsyscate()
    {
    	$id = (int)$this->get[2];
    	$_ENV['cate']->delsyscid($id);
    	$this->update_cache('catesys','sys');
        msg('删除成功！',"index.php?admin_cate");
    }
    
    
    
    
    function onout($msg='') 
    {
    	//var_dump($this->post);exit();
    	//$job = $this->post['job'];
    	$job = ($this->get[2])?$this->get[2]:$this->post['job'];
    	if($job == 'add')
    	{
    		if(!empty($this->post['catename'])) 
    		{
	            $_ENV['cate']->addoutcate($this->post['catename']);
	            msg('添加成功！',"index.php?admin_cate/out");
	        }
	        else
	        {
	        	msg('分类名称不能为空！',"index.php?admin_cate/out");
	        }
    	}
    	elseif ($job == 'addcate')
    	{
    		$cid = $this->post['cid'] ;
            $lines = explode("\n" , $this->post['categorys']);
            $_ENV['cate']->addcidout($lines,$cid);
            msg('添加成功！',"index.php?admin_cate/catelistout/".$cid);
    	}
    	elseif ($job == 'edit'){
    		$id = (isset($this->get[3]))?$this->get[3]:$this->post['id'];
    		$catedata = $_ENV['cate']->get_outcate_byid($id);
    		$cateout = $_ENV['cate']->list_cateout();
    		include template('cateoutlist','admin');
    	}
    	elseif ($job == 'editsave')
    	{
    		$id = $this->post['id'];
    		$name = $this->post['catename'] ;
    		$_ENV['cate']->updateoutcate($name,$id);
            msg('编辑成功！',"index.php?admin_cate/out");
    	}
    	else
    	{
    		$msg && $message=$msg;
    		$cateout = $_ENV['cate']->list_cateout();
        	include template('cateoutlist','admin');
    	}
        
    }
    function ondeloutcate()
    {
    	$id = (int)$this->get[2];
    	$_ENV['cate']->deloutcid($id);
        msg('删除成功！',"index.php?admin_cate/out");
    }
    function oncatelistout()
    {
    	$cid = (isset($this->get[2]))?$this->get[2]:$this->post['id'];
    	$cateout = $_ENV['cate']->list_cateout();
    	$categorylist = $_ENV['cate']->catelist_byoutid($cid);
    	$topname = $_ENV['cate']->get_outcatename($cid);
        include template('catelistout','admin');
    }
    function onreordercateout()
    {
    	$orders = explode(",",$this->post['order']);
    	//var_dump($orders);exit();
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $cid) {
            $_ENV['cate']->order_cateout(intval($cid),$order);
        }
    }
    function onupcateout(){
    	if(isset($this->post['submit'])) 
    	{
    		$cid = $this->post['hiddencid'] ;
            $_ENV['cate']->upcidout($this->post['cname']);
            //$this->oncatelists($cid,"编辑成功");
            msg('添加成功！',"index.php?admin_cate/catelistout/".$cid);
    	}
    }
    function ondelcateout()
    {
    	$id = (int)$this->get[2];
    	$cid = (int)$this->get[3];
    	$_ENV['cate']->delcidout($id);
        msg('删除成功！',"index.php?admin_cate/catelistout/".$cid);
    }
}